package com.xyz.keshe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.xyz.keshe.R;
import com.xyz.keshe.model.News;

import java.util.List;

public class NewsManageAdapter extends BaseAdapter {

    private Context context;
    private List<News> newsList;
    private OnNewsActionListener listener;

    public interface OnNewsActionListener {
        void onView(News news);
        void onEdit(News news);
        void onDelete(News news);
    }

    public NewsManageAdapter(Context context, List<News> newsList, OnNewsActionListener listener) {
        this.context = context;
        this.newsList = newsList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return newsList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news_manage, parent, false);
        }

        News news = newsList.get(position);

        TextView tvTitle = convertView.findViewById(R.id.tv_news_title);
        TextView tvCategory = convertView.findViewById(R.id.tv_news_category);
        TextView tvAuthor = convertView.findViewById(R.id.tv_news_author);
        TextView tvDate = convertView.findViewById(R.id.tv_news_date);
        Button btnView = convertView.findViewById(R.id.btn_view_news);
        Button btnEdit = convertView.findViewById(R.id.btn_edit_news);
        Button btnDelete = convertView.findViewById(R.id.btn_delete_news);

        tvTitle.setText(news.getTitle());
        tvCategory.setText(news.getCategoryName());
        tvAuthor.setText("作者：" + (news.getAuthor() != null ? news.getAuthor() : "未知"));
        tvDate.setText(news.getCreateTime());

        btnView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onView(news);
            }
        });

        btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(news);
            }
        });

        btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(news);
            }
        });

        return convertView;
    }
} 